03. Making a Preference Screen

Making a Preference Screen

Let's add our SettingsActivity, and to launch it, we'll add a menu to EarthquakeActivity!

First order of business, let's add some strings to the res/values/strings.xml file.

In strings.xml:

<!-- Settings Menu Item [CHAR LIMIT=NONE] -->
<string name="settings_menu_item">Settings</string>
<!-- Settings Activity Title [CHAR LIMIT=NONE] -->
<string name="settings_title">Earthquake Settings</string>

Next, let's create the SettingsActivity class.

In SettingsActivity.java:

package com.example.android.quakereport;

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
    }

    public static class EarthquakePreferenceFragment extends PreferenceFragment {

    }
}

And we'll define a layout for the settings activity at res/layout/settings_activity.xml:

In settings_activity.xml:

<fragment
    android:name="com.example.android.quakereport.SettingsActivity$EarthquakePreferenceFragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.quakereport.SettingsActivity">
</fragment>

And of course, we'll need to declare our new activity in AndroidManifest.xml inside the tag:

In AndroidManifest.xml:

<activity
    android:name=".SettingsActivity"
    android:label="@string/settings_title">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.android.quakereport.EarthquakeActivity"/>
</activity>

Menus

It would clutter up our EarthquakeActivity if we put a big SETTINGS button above or below our list of earthquakes. A much better place for that button to live is inside the app bar on the top of the EarthquakeActivity.

An Activity can inflate a menu resource file, and display the menu in the app bar. So next, let's define our menu items in a new menu resource file:

In res/menu/main.xml:

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         tools:context="com.example.android.quakereport.EarthquakeActivity">
         <item
             android:id="@+id/action_settings"
             android:title="@string/settings_menu_item" 
             android:icon="@drawable/ic_filter"
             android:orderInCategory="1"
             app:showAsAction="ifRoom" />
     </menu>

Make sure to also import in the @drawable/ic_filter at the different dpi which can be found here.

And override a couple methods in EarthquakeActivity.java to inflate the menu, and respond when users click on our menu item:

In EarthquakeActivity.java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivity(settingsIntent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Now we've got a menu with a "Settings" item that opens our (blank for now) SettingsActivity!

For more info on menus, check out the Android Developer's Guide at http://developer.android.com/guide/topics/ui/menus.html

If you'd like a summary of these changes, check out https://github.com/udacity/ud843-QuakeReport/commit/900ec8e1e34eb2defc6f1a1f2f13f105873a43d4